package org.dromara.pe.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.core.ExcelResult;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.pe.domain.bo.DeviceScoreRawBo;
import org.dromara.pe.domain.vo.DeviceScoreRawImportVo;
import org.dromara.pe.domain.vo.DeviceScoreRawVo;
import org.dromara.pe.domain.vo.ScopeSettingVo;
import org.dromara.pe.listener.DeviceScoreRawListener;
import org.dromara.pe.service.IDeviceScoreRawService;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * 【请填写功能名称】
 *
 * @author Lion Li
 * @date 2024-09-25
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/pe/deviceScoreRaw")
public class DeviceScoreRawController extends BaseController {

    private final IDeviceScoreRawService DeviceScoreRawService;

    /**
     * 查询【请填写功能名称】列表
     */
    @SaCheckPermission("pe:deviceScoreRaw:list")
    @GetMapping("/list")
    public TableDataInfo<DeviceScoreRawVo> list(DeviceScoreRawBo bo, PageQuery pageQuery) {
        return DeviceScoreRawService.queryPageList(bo, pageQuery);
    }


    /**
     * 导出【请填写功能名称】列表
     */
    @SaCheckPermission("pe:deviceScoreRaw:export")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(DeviceScoreRawBo bo, HttpServletResponse response) {
        List<DeviceScoreRawVo> list = DeviceScoreRawService.queryList(bo);
        ExcelUtil.exportExcel(list, "数据采集记录表", DeviceScoreRawVo.class, response);
    }

    /**
     * 获取导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "运动原始成绩导入模版", DeviceScoreRawImportVo.class, response);
    }

    @Log(title = "导入原始数据", businessType = BusinessType.IMPORT)
    @SaCheckPermission("pe:deviceScoreRaw:import")
    @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(@RequestPart("file") MultipartFile file) throws Exception {
        ExcelResult<DeviceScoreRawImportVo> result = ExcelUtil.importExcel(file.getInputStream(), DeviceScoreRawImportVo.class, new DeviceScoreRawListener(false));
        return R.ok(result.getAnalysis());
    }

    /**
     * 获取【请填写功能名称】详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("pe:deviceScoreRaw:query")
    @GetMapping("/{id}")
    public R<DeviceScoreRawVo> getInfo(@NotNull(message = "主键不能为空")
                                       @PathVariable Long id) {
        return R.ok(DeviceScoreRawService.queryById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
    @SaCheckPermission("pe:deviceScoreRaw:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody DeviceScoreRawBo bo) {
        return toAjax(DeviceScoreRawService.insertByBo(bo));
    }

    /**
     * 修改【请填写功能名称】
     */
    @SaCheckPermission("pe:deviceScoreRaw:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody DeviceScoreRawBo bo) {
        return toAjax(DeviceScoreRawService.updateByBo(bo));
    }

    /**
     * 删除【请填写功能名称】
     *
     * @param ids 主键串
     */
    @SaCheckPermission("pe:deviceScoreRaw:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(DeviceScoreRawService.deleteWithValidByIds(List.of(ids), true));
    }



    /**
     * 查询【请填写功能名称】列表
     */
    @SaCheckPermission("pe:deviceScoreRaw:querySportDataCount")
    @GetMapping("/querySportDataCount")
    public  R<List<DeviceScoreRawVo>> querySportDataCount(DeviceScoreRawBo bo) {
        return R.ok(DeviceScoreRawService.queryList(bo));
    }

/**
 * 1.班级分类，条件男生，女生，周榜，月榜，学期榜
 */


}
